package com.sky.service.impl;

import com.sky.dto.GoodsSalesDTO;
import com.sky.entity.Orders;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.ReportMapper;
import com.sky.mapper.UserMapper;
import com.sky.service.ReportService;
import com.sky.vo.*;
import io.swagger.models.auth.In;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class ReportServiceImpl implements ReportService {
    @Autowired
    private ReportMapper reportMapper;
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private UserMapper userMapper;

    /**
     * 营业额统计
     * @param begin
     * @param end
     * @return
     */
    @Override
    public TurnoverReportVO getTurnoverStatistics(LocalDate begin, LocalDate end) {
        //将时间存储在集合中
        List<LocalDate> dateList = new ArrayList<>();

        dateList.add(begin); //添加开始时间
        while(!begin.equals(end)){
            begin = begin.plusDays(1); //每次存储后一天的日期
            dateList.add(begin);
        }

        List<Double> turnoverList = new ArrayList<>(); //存放每天的营业额

        //根据每一个日期查询营业额 //只查询已完成
        for (LocalDate date : dateList) {
            LocalDateTime beginTime = LocalDateTime.of(date, LocalTime.MIN);
            LocalDateTime endTime = LocalDateTime.of(date,LocalTime.MAX);

            Map map = new HashMap();
            map.put("begin",beginTime);
            map.put("end",endTime);
            map.put("status", Orders.COMPLETED);

            Double turnover = orderMapper.sumByMap(map);
            if(turnover == null){
                turnoverList.add(0.0);
            }else{
                turnoverList.add(turnover);
            }
        }

        return TurnoverReportVO
                .builder()
                .dateList(StringUtils.join(dateList,","))
                .turnoverList(StringUtils.join(turnoverList,","))
                .build();
    }

    /**
     * 用户数据统计
     * @param begin
     * @param end
     * @return
     */
    @Override
    public UserReportVO getUserStatistics(LocalDate begin, LocalDate end) {
        List<LocalDate> dateList = new ArrayList<>(); //存放每天的日期

        dateList.add(begin); //添加开始时间
        while(!begin.equals(end)){
            begin = begin.plusDays(1); //每次存储后一天的日期
            dateList.add(begin);
        }

        List<Integer> newUserList = new ArrayList<>(); //每天新增的用户

        List<Integer> totalUserList = new ArrayList<>(); //总的用户数量

        for (LocalDate date : dateList) {
            LocalDateTime beginTime = LocalDateTime.of(date,LocalTime.MIN);
            LocalDateTime endTime = LocalDateTime.of(date,LocalTime.MAX);

            Map map = new HashMap();
            map.put("end",endTime);

            Integer totalUser = userMapper.countByMap(map); //用户总数量

            map.put("begin",beginTime);

            Integer newUser = userMapper.countByMap(map); //新增用户

            totalUserList.add(totalUser);
            newUserList.add(newUser);
        }
        return UserReportVO
                .builder()
                .dateList(StringUtils.join(dateList,","))
                .totalUserList(StringUtils.join(totalUserList,","))
                .newUserList(StringUtils.join(newUserList,","))
                .build();
    }

    /**
     * 订单统计
     * @param begin
     * @param end
     * @return
     */
    @Override
    public OrderReportVO getOrdersStatistics(LocalDate begin, LocalDate end) {
        List<LocalDate> dateList = new ArrayList<>(); //存放每天的日期

        dateList.add(begin); //添加开始时间
        while(!begin.equals(end)){
            begin = begin.plusDays(1); //每次存储后一天的日期
            dateList.add(begin);
        }

        List<Integer> orderCountList = new ArrayList<>(); //每天的订单数

        List<Integer>  validOrderList = new ArrayList<>(); //每天的有效订单数

        Integer orderCountSum = 0;
        Integer validOrderSum = 0;

        // 遍历集合 查询每天的有效订单数和订单数
        for (LocalDate date : dateList) {
            LocalDateTime beginTime = LocalDateTime.of(date,LocalTime.MIN);
            LocalDateTime endTime = LocalDateTime.of(date,LocalTime.MAX);

            //查询每天的订单数
            Map map = new HashMap();
            map.put("begin",beginTime);
            map.put("end",endTime);

            Integer orderCount = orderMapper.countByMap(map);

            //查询每天的有效订单数

            map.put("status",Orders.COMPLETED);

            Integer validOrder = orderMapper.countByMap(map);

            orderCountSum += orderCount; //累加时间区间内的订单数
            validOrderSum += validOrder; //累加时间区间内的有效订单数
            orderCountList.add(orderCount);
            validOrderList.add(validOrder);
        }
        Double orderCompletionRate = 0.0;
        if(orderCountSum != 0){
            orderCompletionRate = validOrderSum.doubleValue() / orderCountSum; //订单完成率
        }

        return OrderReportVO.builder()
                .dateList(StringUtils.join(dateList,","))
                .orderCountList(StringUtils.join(orderCountList,","))
                .validOrderCountList(StringUtils.join(validOrderList,","))
                .totalOrderCount(orderCountSum)
                .validOrderCount(validOrderSum)
                .orderCompletionRate(orderCompletionRate)
                .build();
    }

    /**
     * 销量排名top10
     * @param begin
     * @param end
     * @return
     */
    @Override
    public SalesTop10ReportVO getTop10(LocalDate begin, LocalDate end) {
        LocalDateTime beginTime = LocalDateTime.of(begin,LocalTime.MIN);
        LocalDateTime endTime = LocalDateTime.of(end,LocalTime.MAX);

        List<GoodsSalesDTO> salesTop10 = orderMapper.getSalesTop10(beginTime,endTime);
        List<String> name = salesTop10.stream().map(GoodsSalesDTO::getName).collect(Collectors.toList());
        String nameList = StringUtils.join(name,",");

        List<Integer> number = salesTop10.stream().map(GoodsSalesDTO::getNumber).collect(Collectors.toList());
        String numberList = StringUtils.join(number,",");
        return SalesTop10ReportVO.builder().numberList(numberList).nameList(nameList).build();
    }
}
